﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class RetrieveListSaleCodeServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveListSaleCodeRequest, RetrieveListSaleCodeResponse>
    {

        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public string TextSearch;
        public FilterRecord Filter = null;
        private UserRecord currentUser;
        public RetrieveListSaleCodeServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveListSaleCodeRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            if (Request.CurrentUser == null)
            {
                throw new ArgumentException("Dao must be set", "request");
            }
            Filter = Request.Filter;
        }

        protected override bool OnExecute(RetrieveListSaleCodeResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            int totalRecord;
            List<SaleCodeInfoRecord> records = new List<SaleCodeInfoRecord>();

            List<DaoCodeS16> listCustomers = new List<DaoCodeS16>();

            listCustomers = Providers.SaleCode.RetrieveListSaleCode(out totalRecord, Request.CurrentUser, Request.Filter);
            records = listCustomers.Select(x => new SaleCodeInfoRecord
            {
                Dao = x.Dao,
                DaoName = x.DaoName,
                StaffId = x.StaffId,
                DeliveryPoint = x.DeliveryPoint,
                Cif = x.Cif,
                FromDate = x.FromDate,
                DeptParent = x.DeptParent
            }).OrderBy(x => x.Dao).Take(10).ToList();

            response.Records = records;

            return isSuccess;
        }
    }
}
